//
//  HPSwiperSelectViewController.m
//  HPZZTong
//
//  Created by handpay on 14-8-14.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPSwiperSelectViewController.h"
#import "HPBorderView.h"
#import "ZZTongDefine.h"
#import <HandPaySwiperModule/HandPaySwiperModule.h>

#define kCellButtonTag   10000;

@interface HPSwiperSelectViewController ()

@property (retain, nonatomic) IBOutlet UITableView *selectTableView;
@property (retain, nonatomic) IBOutlet UIButton *confirmButton;
@property (retain, nonatomic) IBOutlet HPBorderView *tipsBorderView;

@property(retain, nonatomic)NSArray* dataArray;
@property(nonatomic) NSUInteger selectIndex;

@end

// mcm new
typedef NS_ENUM(NSInteger, HPSupportDeviceEnum)
{
    HPSupportDeviceEnumBBPOS_IC,
    HPSupportDeviceEnumBBPOS,
};


@implementation HPSwiperSelectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

// mcm
//- (IBAction)comfirmButtonClick {
//    
//    SwiperType type = _selectIndex + 1;
//    
//    
//    [SwiperService changeSwiperType:type];
//    [HPUIENG popScreen];
//}

// mcm new
-(IBAction)comfirmButtonClick
{
    HPSwiperContext* posCtx = [HPSwiperContext sharedInstance];
    NSDictionary* selectedSwiperDesc = [posCtx.supportDeviceDescriptions objectAtIndex:_selectIndex];
    [posCtx setSelectedSwiperDescription:selectedSwiperDesc];
    [HPUIENG popScreen];
}

- (void)viewDidLoad
{
    self.title = @"刷卡器连接";
    [super viewDidLoad];
    
    _selectTableView.dataSource = self;
    _selectTableView.delegate = self;
    
    [_confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    [_confirmButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    _tipsBorderView.clipsToBounds = NO;
    _tipsBorderView.borderType = BorderTypeSolid;
    _tipsBorderView.dashPattern = 4;
    _tipsBorderView.backgroundColor = RGBCOLOR(220, 220, 220);
    _tipsBorderView.spacePattern = 4;
    _tipsBorderView.borderWidth = 1;
    _tipsBorderView.cornerRadius = 10;
    _tipsBorderView.borderColor = RGBCOLOR(220, 220, 220);
    
    [_tipsBorderView refresh];

    // mcm
    if (!self.dataArray.count)
    {
        self.dataArray = @[@[@"jz01.png", @"音频IC刷卡器"],
                           @[@"jz02.png", @"音频磁条刷卡器"]];
    }
    [self updateUI];
}

- (void)updateUI{
    CGRect frame = _selectTableView.frame;
    frame.size.height = self.dataArray.count * kCellHeight;
    
    _selectTableView.frame = frame;
    _selectTableView.scrollEnabled = NO;
    
    CGRect btnFrame = _confirmButton.frame;
    btnFrame.origin.y = frame.origin.y + frame.size.height + 15;
    _confirmButton.frame = btnFrame;
    
    CGRect borderFrame = _tipsBorderView.frame;
    borderFrame.origin.y = btnFrame.origin.y + btnFrame.size.height + 23;
    _tipsBorderView.frame = borderFrame;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    [_selectTableView release];
    [_confirmButton release];
    [_tipsBorderView release];
    [_dataArray release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setSelectTableView:nil];
    [self setConfirmButton:nil];
    [self setTipsBorderView:nil];
    [self setDataArray:nil];
    [super viewDidUnload];
}
-(void)checkBtnClick:(id)sender {
    UIButton* button = (UIButton*)sender;
    button.selected = !button.selected;
    
    _selectIndex = button.tag - kCellButtonTag;
}

#pragma UITableView dataSourse

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell* cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (indexPath.row == 0) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    
    NSString* iconName = [[self.dataArray objectAtIndex:indexPath.row] objectAtIndex:0];
    
    int left = 15;
    int w = 0;
    int space = 15;
    if (![NSString isNilOrEmpty:iconName]) {
        w = 40;
        UIImageView* iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:iconName]];
        iconImageView.frame = CGRectMake(left, (kCellHeight - w)/2, w, w);
        [cell addSubview:iconImageView];
        [iconImageView release];
        
        left += w + space;
    }
    
    w = 130;
    UILabel* desLabel = [[UILabel alloc] initWithFrame:CGRectMake(left, 0, 130, kCellHeight)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [[self.dataArray objectAtIndex:indexPath.row] objectAtIndex:1];
    [cell addSubview:desLabel];
    [desLabel release];
    
//    left += w + space;
//    
//    w = 40;
//    
//    UIButton* checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [checkButton setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
//    [checkButton setImage:[UIImage imageNamed:@"checkbox2.png"] forState:UIControlStateSelected];
//    checkButton.frame = CGRectMake(left, 0, kCellHeight, kCellHeight);
//    checkButton.tag = kCellButtonTag + indexPath.row;
//    [checkButton addTarget:self action:@selector(checkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    [cell addSubview:checkButton];

    
    return [cell autorelease];
}

//- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
//{
//    if(indexPath.row == _selectIndex){
//        return UITableViewCellAccessoryCheckmark;
//    }
//    else{
//        return UITableViewCellAccessoryNone;
//    }
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(indexPath.row == _selectIndex){
        return;
    }
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_selectIndex
                                                   inSection:0];
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    if (newCell.accessoryType == UITableViewCellAccessoryNone) {
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
    if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        
    }
    _selectIndex = indexPath.row;
}
@end
